package com.cisco.pso;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public enum DiffType {
	Diff {
		@Override
		public String[] extractFileNames(String line, String leftFolder, String rightFolder) {
			//e.g. Files T27L10NSP25/trainingcenter/project.xml and	T28L10NSP8/trainingcenter/project.xml differ

			Pattern p = Pattern.compile("^Files\\s+(.+)\\s+and\\s+(.+)\\s+differ$");
			String[] files = new String[2];
			
	        Matcher m = p.matcher(line); // get a matcher object
	           if(m.find()) {
//	               System.out.println("MATCH: "+m.group());
//	               for(int i=0;i<=m.groupCount();i++)
//	            	   System.out.println(m.group(i));   
//	        
	        	   files[0] = m.group(1);//left
	        	   files[1] = m.group(2);//right
	               
	           }

			return files;
		}
	},
	OnlyIn {
		@Override
		public String[] extractFileNames(String line, String leftFolder, String rightFolder) {
			
			//Only in base/T28L10NSP8/webcomponents/src/java/com/webex/webapp/component/common/taglib: WebcompHtmlMessageTag.java
			
			Pattern p = Pattern.compile("^Only\\s+in\\s+(.+):\\s+(.+)$");
			String[] files = new String[2];
			
	        Matcher m = p.matcher(line); // get a matcher object
	           if(m.find()) {
//	               System.out.println("MATCH: "+m.group());
//	               for(int i=0;i<=m.groupCount();i++)
//	            	   System.out.println(m.group(i));   
	
	               String path = m.group(1);
	               String shortFileName = m.group(2);
	               String fullFileName = path + "/" + shortFileName;
	               if(path.startsWith(leftFolder))
	               {
	            	   files[0] = fullFileName;	            			   
	               }
	               else
	            	   files[1] = fullFileName;
	               

	           }
	           
			   
				return files;

		}
	};

	/**
	 * 
	 * @param str
	 * @return left file name and right file name
	 */
	public abstract String[] extractFileNames(String str, String leftFolder, String rightFolder);
	
	

	public static DiffType getType(String str) {
		if (str.startsWith("Only in"))
			return OnlyIn;
		else if (str.endsWith(" differ")) {
			return Diff;
		} else
			throw new RuntimeException("unknown diff type");

	}
	
	public static void main(String[] args) {
		String[] result;
		
		String line="Files base/T27L10NSP25/trainingcenter/project.xml and base/T28L10NSP8/trainingcenter/project.xml differ";
		result = Diff.extractFileNames(line, "base/T27L10NSP25/", "base/T28L10NSP8/");
		
		line ="Only in base/T28L10NSP8/webcomponents/src/java/com/webex/webapp/component/common/taglib: WebcompHtmlMessageTag.java";
		
		result = OnlyIn.extractFileNames(line, "base/T27L10NSP25/", "base/T28L10NSP8/");
		
	}
}
